﻿@using Smartstore.Web.Models.Topics;

@model TopicModel

@if (Model.Body == null || !Model.Body.Value.HasValue())
{
    return;
}

@if (Model.IsPasswordProtected)
{
    <script sm-target-zone="scripts" data-origin="topic-block">
        $(function () {
            $('#ph-topic-@Model.Id').hide();            
            $('#ph-password-@Model.Id #password-@Model.Id').select().focus();
        });

        function OnAuthenticateSuccess@(Model.Id)(context) {
            if (context.Authenticated)
            {
                $('#ph-title-@Model.Id .topic-html-content-title h2.topic-html-content-header').html(context.Title);
                if ($('#ph-title-@Model.Id .topic-html-content-title h2.topic-html-content-header').text().length == 0)
                {
                    $('#ph-title-@Model.Id').hide();
                }
                $('#ph-topic-@Model.Id .topic-html-content-body').html(context.Body);
                $('#ph-password-@Model.Id').hide();
                $('#ph-topic-@Model.Id').show();
            }
            else
            {
                $('#password-error-@Model.Id').text(context.Error);
                $('#ph-password-@Model.Id #password-@Model.Id').select().focus();
            }
        }
    </script>
    <div id="ph-password-@Model.Id">
        <form sm-ajax method="post" asp-area="" asp-action="Authenticate" asp-controller="Topic"
            sm-onsuccess="OnAuthenticateSuccess@(Model.Id)"
            sm-loading-element-id="#authenticate-progress-@(Model.Id)">
                <input type="hidden" asp-for="Id" />
                <div class="enter-password-title mb-2">
                    @T("Topic.EnterPassword")
                </div>
                <div class="enter-password-form">
                    <div class="input-group">
                        <input type="password" name="password" class="form-control" id="password-@(Model.Id)" />
                        <div class="input-group-append">
                            <button type="submit" class="btn btn-secondary topic-password-button">@T("Topic.Button")</button>
                        </div>
                    </div>
                    <span id="authenticate-progress-@Model.Id" style="display: none;">@T("Common.Wait...")
                        <img src='@Url.Content("~/images/ajax_loader_small.gif")' alt="@T("Common.Wait...")" />
                    </span>
                </div>
                <div class="password-error">
                    <span id="password-error-@Model.Id"></span>
                </div>          
        </form>
    </div>
}

@if (ViewBag.BodyOnly)
{
    <div class="topic-html-content-body@(ViewBag.IsLead == true ? " lead" : "") my-3 html-editor-content" id="ph-topic-body-@Model.Id" sm-language-attributes-for="Model.Body">
        <collapsed-content>
            @Html.Raw(Model.Body)
        </collapsed-content>
    </div>
}
else
{
    <div id="ph-topic-@Model.Id" class="block topic-html-content my-3">
        <div sm-if="Model.Title.HasValue()" class="block-title" id="ph-title-@Model.Id">
			@if (ViewBag.IsLead == true)
			{
				<h1 class="h2" sm-language-attributes-for="Model.Title">@Model.Title</h1>
			}
			else
			{
				<h4 sm-language-attributes-for="Model.Title">@Model.Title</h4>
			}
        </div>
        <div class="block-body html-editor-content topic-html-content-body@(ViewBag.IsLead == true ? " lead" : "")" sm-language-attributes-for="Model.Body">
			<p sm-if="Model.Intro.HasValue() && (ViewBag.IsLead == null || ViewBag.IsLead == false)" class="topic-intro lead">
				@Html.Raw(Model.Intro)
			</p>
            <collapsed-content>
                @Html.Raw(Model.Body)
            </collapsed-content>
        </div>
    </div>
}
